package com.buli.blmall.admin.captcha;

import cn.hutool.captcha.generator.CodeGenerator;

import java.util.Random;

/**
 * 自定义运算验证码
 * @author xiang.gao
 * @date 2024/12/19 15:24
 */
public class MatchCaptcha implements CodeGenerator {

    private int bound = 10;

    private static final String operators = "+-*";

    private int answer;

    public MatchCaptcha() {
    }

    @Override
    public String generate() {
        Random random = new Random();
        // 生成随机运算符
        char operator = operators.charAt(random.nextInt(operators.length()));
        // 生成两个随机数 可以根据需要调整范围
        int num1 = random.nextInt(bound);
        int num2 = random.nextInt(bound);
        switch (operator) {
            case '+':
                answer = num1 + num2;
                break;
            case '-':
                // 保证 num1 >= num2
                if (num1 < num2) {
                    num2 = num2 - num1;
                }
                answer = num1 - num2;
                break;
            case '*':
                answer = num1 * num2;
                break;
            default:
                throw new IllegalStateException("Unexpected value");
        }
        return new StringBuilder().append(num1).append(operator).append(num2).append("=?").toString();
    }

    public int getAnswer() {
        return answer;
    }

    @Override
    public boolean verify(String s, String s1) {
        return false;
    }
}
